package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class StealCourse2Activity extends BaseActivity implements View.OnClickListener {
    private SimpleDraweeView stealphotos_top1;
    private SimpleDraweeView stealphotos_top2;
    private SimpleDraweeView stealphotos_top3;
    private SimpleDraweeView steal_content1, steal_content2, steal_content3,
            steal_content4, steal_content5, steal_content6,
            steal_content7, steal_content8, steal_content9;
    private boolean stealbb1 = false;
    private boolean stealbb2 = false;
    private boolean stealbb3 = false;
    private boolean stealbb4 = false;
    private boolean stealbb5 = false;
    private boolean stealbb6 = false;
    private boolean stealbb7 = false;
    private boolean stealbb8 = false;
    private boolean stealbb9 = false;
    private String url1, url2, url3, url4, url5, url6, url7, url8, url9;

    private TextView stealphotos_change;
    private ImageView stealcourse2_top_text;
    private ImageView stealcourse2_top_tip;
    private List<String> gridpics = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stealcourse2);
        gridpics.add("http://o9im0tspz.bkt.clouddn.com/xiaoyi1.png");
        gridpics.add("http://o9im0tspz.bkt.clouddn.com/xiaoyi2.png");
        gridpics.add("http://o9im0tspz.bkt.clouddn.com/xiaoyi3.png");
        initView();
        registerBoradcastReceiver();
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionStealEachCourseFinish);
        registerReceiver(mListReceiver, myIntentFilter);
    }

    private void ShowTip() {
        if (count == 3) {
            AlertDialog.Builder builder = new AlertDialog.Builder(StealCourse2Activity.this).setMessage("你今天已经偷看了他的三张本地照片了，明天再来吧！")
                    .setNegativeButton(R.string.yn_app_zhidaole, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    }).setPositiveButton(R.string.yn_app_tuichujiaocheng, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            OverCourse();
                            startActivity(new Intent(StealCourse2Activity.this, StealEachPerssionActivity.class));
                            finish();
                        }
                    });
            builder.setCancelable(false);
            builder.show();
        }
    }

    private void OverCourse() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        String urlsto = getString(R.string.SLIVER_URL) + "FinishWatchPhotoTutorial";
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "OverCourse===>" + s.toString());
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                    }
                });
        LiteHttp.newApacheHttpClient(new HttpConfig(StealCourse2Activity.this)).executeAsync(req);
    }

    private void initView() {
        stealphotos_top1 = (SimpleDraweeView) findViewById(R.id.stealphotos_top1);
        stealphotos_top2 = (SimpleDraweeView) findViewById(R.id.stealphotos_top2);
        stealphotos_top3 = (SimpleDraweeView) findViewById(R.id.stealphotos_top3);
        steal_content1 = (SimpleDraweeView) findViewById(R.id.steal_content1);
        steal_content2 = (SimpleDraweeView) findViewById(R.id.steal_content2);
        steal_content3 = (SimpleDraweeView) findViewById(R.id.steal_content3);
        steal_content4 = (SimpleDraweeView) findViewById(R.id.steal_content4);
        steal_content5 = (SimpleDraweeView) findViewById(R.id.steal_content5);
        steal_content6 = (SimpleDraweeView) findViewById(R.id.steal_content6);
        steal_content7 = (SimpleDraweeView) findViewById(R.id.steal_content7);
        steal_content8 = (SimpleDraweeView) findViewById(R.id.steal_content8);
        steal_content9 = (SimpleDraweeView) findViewById(R.id.steal_content9);
        steal_content1.setOnClickListener(this);
        steal_content2.setOnClickListener(this);
        steal_content3.setOnClickListener(this);
        steal_content4.setOnClickListener(this);
        steal_content5.setOnClickListener(this);
        steal_content6.setOnClickListener(this);
        steal_content7.setOnClickListener(this);
        steal_content8.setOnClickListener(this);
        steal_content9.setOnClickListener(this);
        stealphotos_change = (TextView) findViewById(R.id.stealphotos_change);
        stealcourse2_top_text = (ImageView) findViewById(R.id.stealcourse2_top_text);
        stealcourse2_top_tip = (ImageView) findViewById(R.id.stealcourse2_top_tip);
        stealphotos_change.setOnClickListener(this);
    }

    int count = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.steal_content1:
                if (!stealbb1 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content1, StealCourse2Activity.this);
                    url1 = gridpics.get(count);
                    steal_content1.setTag(gridpics.get(count));
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url1);
                    startActivity(intent);
                    stealbb1 = true;
                    count++;
                } else {
                    if (steal_content1.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url1);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content2:
                if (!stealbb2 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content2, StealCourse2Activity.this);
                    url2 = gridpics.get(count);
                    steal_content2.setTag(gridpics.get(count));
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url2);
                    startActivity(intent);
                    stealbb2 = true;
                    count++;
                } else {
                    if (steal_content2.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url2);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content3:
                if (!stealbb3 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content3, StealCourse2Activity.this);
                    steal_content3.setTag(gridpics.get(count));
                    url3 = gridpics.get(count);
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url3);
                    startActivity(intent);
                    stealbb3 = true;
                    count++;
                } else {
                    if (steal_content3.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url3);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content4:
                if (!stealbb4 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content4, StealCourse2Activity.this);
                    steal_content4.setTag(gridpics.get(count));
                    url4 = gridpics.get(count);
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url4);
                    startActivity(intent);
                    stealbb4 = true;
                    count++;
                } else {
                    if (steal_content4.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url4);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content5:
                if (!stealbb5 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content5, StealCourse2Activity.this);
                    url5 = gridpics.get(count);
                    steal_content5.setTag(gridpics.get(count));
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url5);
                    startActivity(intent);
                    stealbb5 = true;
                    count++;
                } else {
                    if (steal_content5.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url5);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content6:
                if (!stealbb6 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content6, StealCourse2Activity.this);
                    url6 = gridpics.get(count);
                    steal_content6.setTag(gridpics.get(count));
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url6);
                    startActivity(intent);
                    stealbb6 = true;
                    count++;
                } else {
                    if (steal_content6.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url6);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content7:
                if (!stealbb7 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content7, StealCourse2Activity.this);
                    url7 = gridpics.get(count);
                    steal_content7.setTag(gridpics.get(count));
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url7);
                    startActivity(intent);
                    stealbb7 = true;
                    count++;
                } else {
                    if (steal_content7.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url7);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content8:
                if (!stealbb8 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content8, StealCourse2Activity.this);
                    url8 = gridpics.get(count);
                    steal_content8.setTag(gridpics.get(count));
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url8);
                    startActivity(intent);
                    stealbb8 = true;
                    count++;
                } else {
                    if (steal_content8.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url8);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.steal_content9:
                if (!stealbb9 && count < 3) {
                    ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(count)), steal_content9, StealCourse2Activity.this);
                    url9 = gridpics.get(count);
                    steal_content9.setTag(gridpics.get(count));
                    updataTopPic(count);
                    Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                    intent.putExtra("url", url9);
                    startActivity(intent);
                    stealbb9 = true;
                    count++;
                } else {
                    if (steal_content9.getTag() != null) {
                        Intent intent = new Intent(StealCourse2Activity.this, StealPhotoCourseBigShowActivity.class);
                        intent.putExtra("url", url9);
                        startActivity(intent);
                    }
                }
                ShowTip();
                break;
            case R.id.stealphotos_change:
                stealbb1 = false;
                stealbb2 = false;
                stealbb3 = false;
                stealbb4 = false;
                stealbb5 = false;
                stealbb6 = false;
                stealbb7 = false;
                stealbb8 = false;
                stealbb9 = false;
                steal_content1.setImageResource(R.drawable.stealcontent_1);
                steal_content2.setImageResource(R.drawable.stealcontent_2);
                steal_content3.setImageResource(R.drawable.stealcontent_3);
                steal_content4.setImageResource(R.drawable.stealcontent_4);
                steal_content5.setImageResource(R.drawable.stealcontent_5);
                steal_content6.setImageResource(R.drawable.stealcontent_6);
                steal_content7.setImageResource(R.drawable.stealcontent_7);
                steal_content8.setImageResource(R.drawable.stealcontent_8);
                steal_content9.setImageResource(R.drawable.stealcontent_9);
                steal_content1.setTag(null);
                steal_content2.setTag(null);
                steal_content3.setTag(null);
                steal_content4.setTag(null);
                steal_content5.setTag(null);
                steal_content6.setTag(null);
                steal_content7.setTag(null);
                steal_content8.setTag(null);
                steal_content9.setTag(null);
                break;
        }
    }

    int clickNum = 0;

    private void updataTopPic(int cc) {
        clickNum++;
//        stealcourse2_top_text.setVisibility(View.GONE);
//        stealcourse2_top_tip.setVisibility(View.VISIBLE);
        switch (count) {
            case 0:
                ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(0)), stealphotos_top1, StealCourse2Activity.this);
                break;
            case 1:
                ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(1)), stealphotos_top2, StealCourse2Activity.this);
                break;
            case 2:
                ImageOptHelper.showFrescoStatus(Uri.parse(gridpics.get(2)), stealphotos_top3, StealCourse2Activity.this);
                break;
        }
    }

    BroadcastReceiver mListReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionStealEachCourseFinish:
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mListReceiver);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_HOME:
                return true;
            case KeyEvent.KEYCODE_BACK:
                return true;
            case KeyEvent.KEYCODE_CALL:
                return true;
            case KeyEvent.KEYCODE_SYM:
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return true;
            case KeyEvent.KEYCODE_VOLUME_UP:
                return true;
            case KeyEvent.KEYCODE_STAR:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
